﻿using System;

using RexToy.ExpressionLanguage;

namespace RexToy.UI.WinForm.ViewModel
{
    public class ProxyCommand : IUICommand
    {
        private string _expr;
        private ExpressionLanguageEngine _engine;
        public ProxyCommand(ExpressionLanguageEngine engine, string expression)
        {
            engine.ThrowIfNullArgument("engine");
            expression.ThrowIfNullArgument("expression");

            _expr = expression.UnBracketing(StringPair.Delayed_Template_Bracket);
            _engine = engine;
        }

        public bool CanExecute()
        {
            var cmd = _engine.Eval<IUICommand>(_expr);
            return cmd.CanExecute();
        }

        public bool SuggestRequery
        {
            get { return true; }
        }

        public void Execute()
        {
            var cmd = _engine.Eval<IUICommand>(_expr);
            cmd.Execute();

            CommandBindRegistry.Instance().RequeryCanExecute();
        }
    }
}
